<Query Kind="Statements">
  <NuGetReference>System.IO.Ports</NuGetReference>
  <Namespace>System.IO.Ports</Namespace>
  <Namespace>System.Threading.Tasks</Namespace>
  <Namespace>BenchmarkDotNet.Attributes</Namespace>
  <RuntimeVersion>8.0</RuntimeVersion>
</Query>

SerialPort _port = new SerialPort("COM2");
_port.Open();

CancellationTokenSource _token_source = new();
CancellationToken _token = _token_source.Token;
using CancellationTokenRegistration _ = _token.Register(() =>
{
	_port.Close();
	_port.Open();
});
_token_source.CancelAfter(2000);

string _reply_message = string.Empty;

try
{
	_port.ReadTo(Environment.NewLine).Dump();
}
catch (Exception ex)
{
	ex.Message.Dump();
}

await Task.Delay(1000);
Console.WriteLine("结束");
_port.Close();